/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.drawingTools;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Bikash HTB
 */
public class DrawHelper {
    
    public List<DrawableFigure> drawableFigureList = new LinkedList<DrawableFigure>();
    public DrawableFigureEnum nowDrawing = DrawableFigureEnum.NONE;
    double firstPointX, firstPointY, secondPointX, secondPointY;
    double pointNumber=0;
    
    public LineDrawable lineDrawable;
    
    public void setDrawType(DrawableFigureEnum nowDrawing)
    {
        firstPointX = secondPointX;
        firstPointY = secondPointY;
        pointNumber = 0;
        this.nowDrawing = nowDrawing;
    }
    
    public void passPoint(double x, double y)
    {
        if(pointNumber == 0)
        {
            firstPointX = x;
            firstPointY = y;
            pointNumber = 1;
        }else if(pointNumber == 1)
        {
            secondPointX = x;
            secondPointY = y;
            
            DrawableFigure figure = null;
            switch(nowDrawing)
            {
                case DRAW_LINE:
                    figure = new LineDrawable(firstPointX, firstPointY, secondPointX, secondPointY);                    
                    break;
                case DRAW_CIRCLE:
                    int rad = (int)Math.sqrt((secondPointX-firstPointX)*(secondPointX-firstPointX) + (secondPointY-firstPointY)*(secondPointY-firstPointY));
                    figure = new CircleDrawable(firstPointX, firstPointY, rad);
                    break;
                case DRAW_RECTANGLE:
                    int ht = (int)Math.abs(secondPointY-firstPointY);
                    int wd = (int)Math.abs(secondPointX-firstPointX);
                    figure = new RectangleDrawable(firstPointX, firstPointY, wd, ht);
                    break;
                case DRAW_POLYGON:
                    figure = new LineDrawable(firstPointX, firstPointY, secondPointX, secondPointY);
                    firstPointX = secondPointX;
                    firstPointY = secondPointY;
                    //pointNumber = 1;
                    break;
                case NONE:
                    figure = new LineDrawable(firstPointX, firstPointY, secondPointX, secondPointY); 
                    break;
            }
            if(figure != null){
                drawableFigureList.add(figure);
            } 
            if(nowDrawing == DrawableFigureEnum.DRAW_POLYGON){
                pointNumber = 1; 
            } else{
                pointNumber = 0;
            }
                
            
           
        }
    }//end of method
    
    public void draw(Graphics g){
        //previous stroke save kore rakhben
        //mota ekta stroke diben
        Graphics2D g2d = (Graphics2D)g;
        Color c = g.getColor();
        Stroke s = g2d.getStroke();
        g2d.setStroke(new BasicStroke(3));
        g.setColor(Color.RED);
        for(DrawableFigure figure : drawableFigureList){
            figure.draw(g2d);
        }//end for
        //stroke resest korben 
        g2d.setStroke(s);
        g.setColor(c);
    }//end of draw
    
   
}//end of class
